package org.j4me.deviceinfo.data;

import java.util.Comparator;

import org.dom4j.Element;

public class Jsr implements Comparable<Jsr> {
	private String _jsr;
	private boolean _has;

	public Jsr(String jsr, String has) {
		_jsr = jsr;
		try {
			_has = has.equals("true");
		} catch (Exception e) {
		}
	}

	public Jsr(Element elt) {
		_jsr = elt.attributeValue("id");
		try {
			_has = elt.attributeValue("has").equals("1");
		} catch (Exception e) {
		}
	}

	public void toXml(Element elt) {
		elt.addAttribute("id", _jsr);
		elt.addAttribute("has", _has ? "1" : "0");
	}

	
	public boolean equals(Object obj) {
		if (obj instanceof Jsr) {
			Jsr jsr = (Jsr) obj;
			return (_has == jsr._has && (_jsr == jsr._jsr || (_jsr != null && _jsr.equals(jsr._jsr))));
		}
		return super.equals(obj);
	}

	public String getJsr() {
		return _jsr;
	}

	public boolean isHas() {
		return _has;
	}


	public int getJsrValue()
	{
		try{
			return Integer.parseInt(_jsr);
		} catch (Exception e) {
			try
			{
				return Integer.parseInt(_jsr.substring(0,_jsr.indexOf('_')));
			}catch(Exception e2)
			{
				return 1000;
			}
		}
	}
	public int compareTo(Jsr o) {
		
		int dif = getJsrValue() - o.getJsrValue();
		if(dif == 0)
			return _jsr.compareTo(o._jsr);
		return dif;
	}
	
}
